<!--
 * @author luguoxiang
 * @date 2022/9/17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <edit-container ref="containerRef" v-bind="containerConfig">
    <lg-form ref="formRef" v-loading="loading" :schema="schema" v-bind="formConfig">
      <template #columnName="{ data }">
        <t-auto-complete
          v-model="data.columnName"
          style="width: 100%"
          :options="options"
          highlight-keyword
          placeholder="请输入列名"
          clearable
          autofocus
        />
      </template>
      <template #columnValue="{ data }">
        <t-input-adornment>
          <template #prepend>
            <lg-dict v-model="data.provideType" code="ProvideTypeEnum" placeholder="请选择" style="width: 100px" />
          </template>
          <template v-if="data.provideType === 2">
            <t-select
              v-model="data.columnValue"
              :options="provides"
              clearable
              placeholder="请选择"
              @change="onchange"
            />
          </template>
          <template v-else-if="data.provideType === 1">
            <t-select
              v-model="data.columnValue"
              :options="SystemValueEnum"
              clearable
              placeholder="请选择"
              @change="onchange"
            />
          </template>
          <template v-else>
            <t-input v-model="data.columnValue" @change="onchange" />
          </template>
        </t-input-adornment>
      </template>
    </lg-form>
  </edit-container>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { EditContainer, useEditContainer } from '@/components';
import { LgDict, LgForm, toOptions } from '@/soar';
import schema from './schema';
import api from './api';
import ruleProvideApi from './ruleProvideApi.ts';
import { enums } from '@/api/common';

const options = [
  'create_by',
  'update_by',
  'create_time',
  'update_time',
  'org_id',
  'user_id',
  'disabled',
  'status',
  'type',
  'name',
  'code',
];

const emits = defineEmits(['ok']);

const containerRef = ref();
const formRef = ref();

const { containerConfig, formConfig, loading, open } = useEditContainer<any>({
  header: '数据规则明细',
  containerRef,
  formRef,
  getData: (params: any) => api.detail(params.id),
  onOk: async ({ values, data, params, isUpdate, changed }) => {
    if (changed) {
      values.id = data.id;
      await api.saveOrUpdate(values, isUpdate);
      emits('ok', values);
    }
  },
  containerProps: {
    width: '600px',
  },
});

const SystemValueEnum = ref([]);
enums('SystemValueEnum').then((res) => {
  SystemValueEnum.value = toOptions(res.SystemValueEnum);
});

const provides = ref([]);
ruleProvideApi.query({ queryType: 'list', pageSize: 9999 }).then((res) => {
  provides.value = res.list.map((x: { id: any; name: any }) => ({ value: x.id, label: x.name }));
});
defineExpose({ open });
</script>
<style lang="less" scoped>
.t-form-item__columnValue {
  .t-input-adornment {
    flex: 1;
  }
}
</style>
